//Copyright 2011 New York City 3D Community

//This file is part of New York City 3D.

//New York City 3D is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//New York City 3D is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with New York City 3D.  If not, see <http://www.gnu.org/licenses/>.

package nyc3d;

import nyc3d.vehicle.Vehicle;
import com.jme3.collision.CollisionResults;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.system.AppSettings;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class HUD{
    private String currentStreet, currentTime;
    private AppSettings settings;
    private final String DATE_FORMAT = "HH:mm";
    private Screen hudScreen;

    public HUD(AppSettings settings){
        this.settings = settings;
    }

    public void update(ArrayList<Node> nodes, Camera cam, Vehicle currentCar, Nifty nifty){
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        currentTime = sdf.format(cal.getTime());
        Ray ray;
        
        CollisionResults results = new CollisionResults();

        if (currentCar != null)
            ray = new Ray(currentCar.getCarNode().getWorldTranslation(), new Vector3f(0, -1, 0));
        else
            ray = new Ray(cam.getLocation(), new Vector3f(0, -1, 0));
 
        nodes.get(0).updateGeometricState();
        nodes.get(1).collideWith(ray, results);

        if (results.size() > 0 && results.getClosestCollision().getDistance() < 500)
            currentStreet = results.getClosestCollision().getGeometry().getName().substring(0, results.getClosestCollision().getGeometry().getName().indexOf("(")).replace('_', ' ').replace(" Cross", "");
        else
            currentStreet = "";
    }

    public AppSettings getSettings() {
        return settings;
    }

    public void setSettings(AppSettings settings) {
        this.settings = settings;
    }

    public String getCurrentStreet() {
        return currentStreet;
    }

    public void setCurrentStreet(String currentStreet) {
        this.currentStreet = currentStreet;
    }

    public Screen getHudScreen() {
        return hudScreen;
    }

    public void setHudScreen(Screen hudScreen) {
        this.hudScreen = hudScreen;
    }

    public String getCurrentTime() {
        return currentTime;
    }

    public void setCurrentTime(String currentTime) {
        this.currentTime = currentTime;
    }
}